package com.qingxin.cms.service;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qingxin.cms.entity.QxNotices;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.entity.MyUploadFileParam;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;

@Service
public class QxNoticesService extends BaseService<QxNotices, String> {
	
	@Autowired
	private QxNoticesAttachmentsService attachmentsService;
	
	
	public Pager list(String search, PageParam pageParam, HttpServletRequest request){
		Querier querier = Querier.create("select new com.qingxin.cms.entity.QxNotices(" +
				"n.noticeid, n.title, n.noticeType, n.pubUser, n.pubDate, n.createTime)" +
				" from QxNotices n ");
		if(StringUtils.isNotEmpty(search)){
			search = "%"+search.replace("'", "")+"%";
			querier.append(" where n.title like :title ");
			querier.setParam("title", search);
		}
		querier.append(" order by n.createTime desc");
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	
	public QxNotices getLastest(){
		QxNotices notices = (QxNotices) getByHql("from QxNotices n order by n.createTime desc");
		return notices;
	}
	
	
	
	/**
	 * 添加
	 * @param notices
	 * @param uploadFileParam
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(QxNotices notices, MyUploadFileParam uploadFileParam, LoginUser loginUser, HttpServletRequest request){
		notices.setCreateBy(loginUser.getLogin_name());
		notices.setCreateTime(new Date());
		notices.setUpdateBy(loginUser.getLogin_name());
		notices.setUpdateTime(new Date());
		
		notices.setNoticeType(0);
		
		this.save(notices);
		
		//添加附件 
		attachmentsService.addAttachments(notices.getNoticeid(), uploadFileParam, loginUser);
		
		return null;
	}
	
	/**
	 * 修改
	 * @param notices
	 * @param uploadFileParam
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(QxNotices notices, MyUploadFileParam uploadFileParam, LoginUser loginUser, HttpServletRequest request){
		QxNotices old = getSafeEntity(notices.getNoticeid());
		
		String fields = "title,contents,pubUser,pubDate";
		BeanUtil.setTargetFields(old, notices, fields, true);
		
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(new Date());
		
		Updater<QxNotices> updater = new Updater<>(old, fields+",updateBy,updateTime");
		this.update(updater);
		
		attachmentsService.addAttachments(notices.getNoticeid(), uploadFileParam, loginUser);
		
		return null;
	}
	
	/**
	 * 删除
	 * @param noticeids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError delete(String[] noticeids, HttpServletRequest request){
		for(String noticeid : noticeids){
			QxNotices notices = this.get(noticeid);
			if(notices == null){
				continue;
			}
			this.delete(notices);
		}
		return null;
	}
	
}
